package dao

import "time"

type image_store struct {
	IsId int `json:"is_id" gorm:"column:is_id"`
	Uid int `json:"uid"`
	Hostname string `json:"hostname" gorm:"column:hostname"`
	Username string `json:"username" gorm:"column:username"`
	SshPort float64 `json:"ssh_port" gorm:"column:ssh_port"`
	Uuid string `json:"uuid" gorm:"column:uuid"`
	Name string `json:"name" gorm:"column:name"`
	Url string `json:"url" gorm:"column:url"`
	TotalCapacity float64 `json:"total_capacity" gorm:"column:total_capacity"`
	AvailableCapacity float64 `json:"available_capacity" gorm:"column:available_capacity"`
	Type string `json:"type" gorm:"column:type"`
	State string `json:"state" gorm:"column:state"`
	Status string `json:"status" gorm:"column:status"`
	ZoneUuid string `json:"zone_uuid" gorm:"column:zone_uuid"`
	Ctime int64 `json:"c_time" gorm:"column:c_time"`
	Dtime int64 `json:"d_time" gorm:"column:d_time"`
}
func (t *image_store) Create(d map[string]interface{}) (bool,int)  {

	var addis image_store
	if v,ok := d["hostname"];ok{
		addis.Hostname = v.(string)
	}
	if v,ok := d["username"];ok{
		addis.Username = v.(string)
	}
	if v,ok := d["ssh_port"];ok{
		addis.SshPort = v.(float64)
	}
	if v,ok := d["uuid"];ok{
		addis.Uuid = v.(string)
	}
	if v,ok := d["zone_uuid"];ok{
		addis.ZoneUuid = v.(string)
	}
	if v,ok := d["name"];ok{
		addis.Name = v.(string)
	}
	if v,ok := d["url"];ok{
		addis.Url = v.(string)
	}
	if v,ok := d["total_capacity"];ok{
		addis.TotalCapacity = v.(float64)
	}
	if v,ok := d["available_capacity"];ok{
		addis.AvailableCapacity = v.(float64)
	}
	if v,ok := d["type"];ok{
		addis.Type = v.(string)
	}
	if v,ok := d["state"];ok{
		addis.State = v.(string)
	}
	if v,ok := d["status"];ok{
		addis.Status = v.(string)
	}
	if v,ok := d["uid"];ok{
		addis.Uid = v.(int)
	}

	addis.Ctime = time.Now().Unix()

	err := db.Create(&addis).Error
	b := true
	if err != nil{
		b = false
	}
	var id []int
	db.Raw("select LAST_INSERT_ID() as id").Pluck("id", &id)
	return b,id[0]

}
func (t *image_store)Delete(w map[string]interface{}) bool {

	err := db.Where(w).Delete(&t).Error
	b := true
	if err != nil{
		b = false
	}
	return b

}
var ImageServer = &image_store{}
